<?php
declare (strict_types=1);
/**
 * @Description:
 * @Author: Chern
 * @Date: 2023/7/11
 * @Time: 10:49
 */

namespace app\model;

use app\service\System;
use app\stateData;
use think\Model;

class SystemMenu extends Model
{

    /**
     * 菜单管理类
     * @var string
     */

    protected $createTime = 'create_date';

    public function getTypeAttr($name): array
    {
        return [
            'id'=>$name,
            'text'=>stateData::$_template_type[$name],
        ];
    }

    public function getMenuList($where=''): array
    {
        return SystemMenu::whereLike('menu_name','%'.$where.'%')->order('sort ASC')->select()->toArray();
    }

    public function getMenuIconAttr($value)
    {
        return str_replace('\\', '/', 'http://'.$_SERVER['HTTP_HOST'].'/storage/'.$value);
    }

    public function getUrlAttr($value,$data)
    {
        return $data['route_url'];
    }


    /**
     * 添加菜单
     * @param $data
     * @return SystemMenu|Model
     */

    public function addMenuModel($data)
    {
        return SystemMenu::create($data);
    }

    /**
     * 修改菜单内容
     * @param $data
     * @return SystemMenu
     */
    public function updateMenuModel($data)
    {
        return SystemMenu::update($data);
    }

    /**
     * 删除菜单
     * @param $id
     * @return bool
     */
    public function delMenuModel($id)
    {
        return SystemMenu::destroy($id);
    }


    /**
     * 根据alias_id 获取menu_name字段
     * @param $alId
     * @return SystemMenu[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMenuName($alId)
    {
        return SystemMenu::where('alias_id','in',$alId)->select();
    }


    /**
     * 获取level对应等级的菜单
     * @param $level
     * @return SystemMenu[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getLevelMenuModel($level)
    {
        return $this->whereIn('menu_level',$level)->select();
    }

    /**
     * 查询type为0的菜单
     * @param $type
     * @return \app\model\SystemMenu[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getTypeMenuModel($type=0){
        return $this->where (['type'=>$type])->select();
    }

    /**
     * 通过菜单别名找菜单详情
     * @param $aliasId
     * @return \app\model\SystemMenu|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAliasIdModel($aliasId)
    {
        return $this->where(['alias_id'=>$aliasId])->find();
    }

    /**
     * 获取菜单菜单id
     * @return array
     */
    public function getAllMenuId(): array
    {
        return $this->column ('id');
    }

    public function getSonMenu($id)
    {
        return $this->where(['parent_id'=>$id])->find ();
    }

    /**
     * 获取菜单别名对应id
     * @param $aliasId
     * @return array
     */
    public function getAliasIdMenu($aliasId)
    {
        return $this->where (['alias_id'=>$aliasId])->value ('id');
    }

    public function getAliasIdSonDataModel($id):array
    {
        return $this->where (['parent_id'=>$id])->select()->toArray ();
    }

    /**
     * 通过别名查询父级别名
     * @param $alias
     * @return mixed
     */
    public function getParentAliasModel($alias)
    {
        $id = $this->where (['alias_id'=>$alias])->value ('parent_id');
        if (!$id){
            return "";
        }
        return $this->where (['id'=>$id])->value ('alias_id');
    }
}